//
//  simHomeViewController.m
//  simens
//
//  Created by 稳 张 on 12-7-29.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "simHomeViewController.h"
#import "simShopVisitPlanController.h"
#import "simShopVisitCalendarController.h"
#import "simMessageViewController.h"
#import "simShopVisitCalendarController.h"
#import "simTodayVisitorViewController.h"
#import "CustomTabBar.h"


@interface simHomeViewController ()<CustomTabBarDelegate,simHomeChangedDelegate> {
@private
    NSArray* _tabBarItems ;
    BOOL _hideBottomBecauseOfPresent;
    
}

@property (strong, retain) CustomTabBar* tabBar;

@property(nonatomic, assign) UIViewController *selectedViewController;

@property (strong) simShopVisitPlanController* simPlanController;


- (void) hideTabBar :(BOOL) hide;

@end

#define SELECTED_VIEW_CONTROLLER_TAG 98456345

@implementation simHomeViewController

@synthesize selectedIndex = _selectedIndex;
@synthesize selectedViewController = _selectedViewController ;
@synthesize tabBar = _tabBar;
@synthesize lastSelectedIndex = _lastSelectedIndex;
@synthesize delegate = _delegate ;
@synthesize calendarController = _calendarController;
@synthesize simPlanController = _simPlanController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (id) init {
    
    self = [super init];
    
    if (self) {
        NSNotificationCenter  *center = [NSNotificationCenter defaultCenter];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hidesCustomerBottomBarWhenPushed" object:nil];
        [center addObserver:self selector:@selector(hidesCustomerBottomBarWhenPushed:) name:@"hidesCustomerBottomBarWhenPushed" object:nil];
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hidesCustomerBottomBarWhenPresent" object:nil];
        [center addObserver:self selector:@selector(hidesCustomerBottomBarWhenPresent) name:@"hidesCustomerBottomBarWhenPresent" object:nil];
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showCustomerBottomBarWhenDismiss" object:nil];
        [center addObserver:self selector:@selector(showCustomerBottomBarWhenDismiss) name:@"showCustomerBottomBarWhenDismiss" object:nil];
        
        return  self;
    }
    return nil;
    
}

#pragma mark -
#pragma mark CustomTabBarDelegate

- (UIImage*) imageFor:(CustomTabBar*)tabBar atIndex:(NSUInteger)itemIndex {
    // Get the right data
    NSDictionary* data = [_tabBarItems objectAtIndex:itemIndex];
    // Return the image for this tab bar item
    return [UIImage imageNamed:[data objectForKey:@"image"]];
}


- (UIImage*) imagedFor:(CustomTabBar*)tabBar atIndex:(NSUInteger)itemIndex {
    // Get the right data
    NSDictionary* data = [_tabBarItems objectAtIndex:itemIndex];
    // Return the image for this tab bar item
    return [UIImage imageNamed:[data objectForKey:@"imaged"]];
}


- (void) loadView {
    
    [super loadView];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UINavigationController *hot1 = [[UINavigationController alloc] initWithRootViewController: [[simShopVisitPlanController alloc] init]];
    
    [hot1.navigationBar setNeedsDisplay];
    
    
    
    self.simPlanController = [[simShopVisitPlanController alloc] init];
    [self.simPlanController setHomePageDelegate:self];
    UINavigationController *hot2 = [[UINavigationController alloc]initWithRootViewController:
                                      self.simPlanController]; 
    
    self.calendarController = [[simShopVisitCalendarController alloc] init];
    [self.calendarController setHomePageDelegate:self];
    
    UINavigationController *hot3 = [[UINavigationController alloc]initWithRootViewController:
                                      self.calendarController ]; 
    
    UINavigationController *hot4 = [[UINavigationController alloc]initWithRootViewController:
                                      [[simTodayVisitorViewController alloc] init] ]; 
    
    UINavigationController *hot5 = [[UINavigationController alloc]initWithRootViewController:
                                    [[simMessageViewController alloc] init] ]; 
    
    _tabBarItems = [NSArray arrayWithObjects:
                    [NSDictionary dictionaryWithObjectsAndKeys:@"bfqd.png", @"image", @"bfqd_selected.png", @"imaged",hot1, @"viewController", nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"jhbf.png", @"image", @"jhbf_selected.png", @"imaged",hot2, @"viewController", nil],
                    
                    [NSDictionary dictionaryWithObjectsAndKeys:@"home.png", @"image", @"home_selected.png", @"imaged",hot3, @"viewController", nil], 
                    
                    [NSDictionary dictionaryWithObjectsAndKeys:@"drbf.png", @"image", @"drbf_selected.png", @"imaged",hot4, @"viewController", nil],
                    
                    [NSDictionary dictionaryWithObjectsAndKeys:@"wdzx.png", @"image", @"wdzx_selected.png", @"imaged",hot5, @"viewController", nil], nil];
    
    //self.navigationItem.titleView = ;
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    
    self.tabBar = [[CustomTabBar alloc] initWithItemCount:_tabBarItems.count itemSize:CGSizeMake(self.view.frame.size.width/_tabBarItems.count, 40) tag:0 delegate:self];
    
    [self.tabBar setBackgroundColor:[UIColor clearColor]];
    // Place the tab bar at the bottom of our view
    _tabBar.frame = CGRectMake(0,self.view.frame.size.height-40,self.view.frame.size.width, 40);
    [self.view addSubview:_tabBar];
    
    // Select the first tab
    [_tabBar selectItemAtIndex:2];
    [self touchDownAtItemAtIndex:2];
    [self.view bringSubviewToFront:_tabBar];
    NSLog(@"%f",self.view.frame.size.height);
    
    

    
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void) touchDownAtItemAtIndex:(NSUInteger)itemIndex {
    
    _lastSelectedIndex = _selectedIndex;
    _selectedIndex = itemIndex;
    // Remove the current view controller's view
    
    for (UIView *v in [self.view subviews]) {
        if (v.tag == SELECTED_VIEW_CONTROLLER_TAG ) {
            [v removeFromSuperview];
        }
    } 
    
    // Get the right view controller
    NSDictionary* data = [_tabBarItems objectAtIndex:itemIndex];
    UIViewController* viewController = [data objectForKey:@"viewController"]; 
    
    _selectedViewController = viewController ;
    
    viewController.view.hidden = NO;
    // Set the view controller's frame to account for the tab bar 
    viewController.view.frame = CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height - 40); 
    //    viewController.view.frame = self.view.frame;
    
    // Se the tag so we can find it later
    viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
    
    // Add the new view controller's view
    [self.view insertSubview:viewController.view belowSubview:_tabBar];
    
    // In 1 second glow the selected tab
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(addGlowTimerFireMethod:) userInfo:[NSNumber numberWithInteger:itemIndex] repeats:NO];
    
    SEL sel = @selector(tabBarController:didSelectIndex:);
    
    if (self.delegate && [self.delegate respondsToSelector:sel]) 
    {  
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self.delegate performSelector:sel withObject:self withObject: [NSNumber numberWithInteger:_selectedIndex]];
#pragma clang diagnostic pop
        
    } 
    
    
    SEL sel2 = @selector(tabBarController:didSelectViewController:);
    
    if (self.delegate && [self.delegate respondsToSelector:sel2]) 
    {  
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self.delegate performSelector:sel2 withObject:self withObject:_selectedViewController];
#pragma clang diagnostic pop
        
    } 
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"EtaoHomeViewController" object:[NSString stringWithFormat:@"yes"]]; 
}


- (void)addGlowTimerFireMethod:(NSTimer*)theTimer {
    // Remove the glow from all tab bar items
    for (NSUInteger i = 0 ; i < _tabBarItems.count ; i++)
    {
        [_tabBar removeGlowAtIndex:i];
    }
    
    // Then add it to this tab bar item
    [_tabBar glowItemAtIndex:[[theTimer userInfo] integerValue]];
}


- (void) hideTabBar :(BOOL) hide {
    
}

- (void) touchUpInsideItemAtIndex:(NSUInteger)itemIndex {
    
}


- (UIImage*) backgroundImage {
    
//    UIView* shadingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
//    shadingView.color1 = [UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1.0f];
//    shadingView.color2 = [UIColor colorWithRed:47.0/255 green:47.0/255 blue:47.0/255 alpha:1.0f];
//    //    UIImage* image = [shadingView getImage];
    return [UIImage imageNamed:@"bottomToobar_week.png"];
    
}

- (UIImage*) tabBarArrowImage {
    // 暂时不用
    // return [UIImage imageNamed:@"more.png"];
    return nil;
}

// This is the blue background shown for selected tab bar items
- (UIImage*) selectedItemBackgroundImage {
    return nil; 
}


// This is the glow image shown at the bottom of a tab bar to indicate there are new items
- (UIImage*) glowImage { 
    return nil; 
}

- (UIImage*) selectedItemImage {     
     return [UIImage imageNamed:@"favority.png"];   
}

- (void)changedToIndex:(int)index {
    [self touchDownAtItemAtIndex:index];
    
    if (index == 2) {
        [self.calendarController updateFromHttp];
    }

}


//- (UIImage*) backgroundImage {}
//- (UIImage*) selectedItemBackgroundImage{}
//- (UIImage*) glowImage{}
//- (UIImage*) selectedItemImage{}
//- (UIImage*) tabBarArrowImage{}


@end
